﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vowei.Core.Models;
using Vowei.Web.Helpers;
using Vowei.Web.Models;
using Vowei.Data;

namespace vowei.test
{
    /// <summary>
    /// WoorkflowTest 的摘要说明
    /// </summary>
    [TestClass]
    public class WorkflowTest : TestBase
    {
        private class IUserComparer : IEqualityComparer<IUser>
        {
            #region IEqualityComparer<IUser> Members

            public bool Equals(IUser x, IUser y)
            {
                if (object.Equals(x, y))
                    return true;
                else if (x != null && y != null)
                    return string.CompareOrdinal(x.Email, y.Email) == 0;
                else
                    return false;
            }

            public int GetHashCode(IUser obj)
            {
                return obj.GetHashCode();
            }

            #endregion
        }

        [TestMethod]
        public void CreateCollectFeedbackTaskTest()
        {
            using (var impl = new VoweiContextImpl())
            {
                var project = GetSvnProject(impl);;
                var task = new Task()
                {
                    Title = "完成收集反馈工作流的单元测试工作",
                    AssignedTo = Setting.DefaultUserName,
                    Description = "测试收集反馈工作流,用来设计收集反馈工作流的API",
                    DueDate = DateTime.Now + TimeSpan.FromDays(1),
                    // TODO: 对于需要review的任务，进度都是90%，取“行百里者，半于九十之意”
                    Progress = 0.9f,
                    StartDate = DateTime.Now
                };

                Assert.IsNotNull(project.TeamMembers);
                Assert.AreNotEqual(0, project.TeamMembers.Count());

                var reviewers = project.TeamMembers.ToArray();
                WorkflowHost.CreateCollectFeedbackFlow(task, 
                var cftasks = project.CreateCollectFeedbackFlow(task, reviewers);

                Assert.IsTrue(cftasks.All(t => t.ClosedDate == null));
                Assert.AreEqual(reviewers.Length - 1, cftasks.Length);
                Assert.IsTrue(cftasks.All(
                    t => reviewers.Any(
                    r => string.CompareOrdinal(t.AssignedTo, r.Email) == 0)));
            }
        }

        //[TestMethod]
        //public void CreateCollectFeedbackWithoutTaskOwnerTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "CreateCollectFeedbackWithoutTaskOwnerTest",
        //            AssignedTo = Setting.DefaultUserName,
        //            Description = "测试收集反馈工作流,用来设计收集反馈工作流的API，将任务的负责人从审批人员列表里面删除",
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            // TODO: 对于需要review的任务，进度都是90%，取“行百里者，半于九十之意”
        //            Progress = 0.9f,
        //            StartDate = DateTime.Now
        //        };

        //        Assert.IsNotNull(project.TeamMembers);
        //        Assert.AreNotEqual(0, project.TeamMembers.Count());

        //        var team = project.TeamMembers.ToArray();
        //        var reviewers = team.Except(new User[] { 
        //        new User() { Email = task.AssignedTo } },
        //            new IUserComparer());
        //        var cftasks = project.CreateCollectFeedbackFlow(task, reviewers.ToArray());

        //        Assert.IsTrue(cftasks.All(t => t.ClosedDate == null));
        //        Assert.AreEqual(reviewers.Count(), cftasks.Length);
        //        Assert.IsTrue(cftasks.All(
        //            t => reviewers.Any(
        //            r => string.CompareOrdinal(t.AssignedTo, r.Email) == 0)));
        //    }
        //}

        //[TestMethod]
        //public void GetCollectFeedbackWorkflowTaskProgressTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "GetCollectFeedbackWorkflowProgressTest_1",
        //            AssignedTo = Setting.DefaultUserName,
        //            Description = "测试收集反馈工作流的进度",
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 1,
        //            StartDate = DateTime.Now
        //        };
        //        var reviewers = project.TeamMembers.ToArray();
        //        var cftasks = project.CreateCollectFeedbackFlow(task, reviewers);

        //        Assert.AreEqual(0.9, task.Progress);

        //        task = new Task()
        //        {
        //            Title = "GetCollectFeedbackWorkflowProgressTest_1",
        //            AssignedTo = Setting.DefaultUserName,
        //            Description = "测试收集反馈工作流的进度",
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 0.8f,
        //            StartDate = DateTime.Now
        //        };
        //        cftasks = project.CreateCollectFeedbackFlow(task, reviewers);
        //        Assert.AreEqual(0.9, task.Progress);
        //    }
        //}

        //[TestMethod]
        //public void GetCollectFeedbackWorkflowProgressTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "GetCollectFeedbackWorkflowProgressTest",
        //            AssignedTo = Setting.DefaultUserName,
        //            Description = "测试收集反馈工作流的进度",
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 1,
        //            StartDate = DateTime.Now
        //        };
        //        var reviewers = project.TeamMembers.ToArray();
        //        var cftasks = project.CreateCollectFeedbackFlow(task, reviewers);

        //        foreach (var cftask in cftasks)
        //        {
        //            cftask.Close();
        //        }

        //        context.SaveChanges();
        //        Assert.AreEqual(1.0f, task.Progress);
        //        Assert.IsTrue(task.ClosedDate.HasValue);
        //    }
        //}

        //[TestMethod]
        //public void GetNotFinishedCollectFeedbackWorkflowProgressTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "GetNotFinishedCollectFeedbackWorkflowProgressTest",
        //            AssignedTo = Setting.DefaultUserName,
        //            Description = "测试收集反馈工作流的进度",
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 1,
        //            StartDate = DateTime.Now
        //        };
        //        var reviewers = project.TeamMembers.ToArray();
        //        var cftasks = project.CreateCollectFeedbackFlow(task, reviewers);

        //        for (int i = 0; i < cftasks.Length - 1; ++i)
        //            cftasks[i].Close();

        //        context.SaveChanges();

        //        // TODO: 前面一个9代表任务尚未完成，后面一个9代表审核任务尚未完成
        //        Assert.AreEqual(0.99f, task.Progress);
        //    }
        //}

        //[TestMethod]
        //public void CreateApprovalTaskTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "待审批的任务",  // Title
        //            Description = "测试审批任务的API设计", // Description
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 0.9f
        //        };

        //        var approvers = new IUser[] {
        //        new User() { Email = "manager@test.com" },
        //        new User() { Email = "director@test.com" },
        //        new User() { Email = "vp@test.com" }
        //    };

        //        // 创建一个简单的审批流程
        //        var caTasks = project.CreateApprovalFlow(task, approvers);
        //        Assert.AreEqual(approvers.Length, caTasks.Length);

        //        for (int i = 0; i < approvers.Length; ++i)
        //        {
        //            Assert.AreEqual(approvers[i], caTasks[i].AssignedTo);
        //        }
        //    }
        //}

        //[TestMethod]
        //public void ApprovalTaskProgressTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "ApprovalTaskProgressTest_1",  // Title
        //            Description = "测试被审批的任务的进度", // Description
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 1.0f
        //        };

        //        var approvers = new IUser[] {
        //        new User() { Email = "manager@test.com" },
        //        new User() { Email = "director@test.com" },
        //        new User() { Email = "vp@test.com" }
        //    };

        //        // 创建一个简单的审批流程
        //        var caTasks = project.CreateApprovalFlow(task, approvers);
        //        Assert.AreEqual(0.9f, task.Progress);

        //        task = new Task()
        //        {
        //            Title = "ApprovalTaskProgressTest_2",  // Title
        //            Description = "测试被审批的任务的进度", // Description
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 0.8f
        //        };

        //        // 创建一个简单的审批流程
        //        caTasks = project.CreateApprovalFlow(task, approvers);
        //        Assert.AreEqual(0.9f, task.Progress);
        //    }
        //}

        //[TestMethod]
        //public void OverrideApprovalTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "OverrideApprovalTest_1",  // Title
        //            Description = "测试具有高层级审批权限的人员做出审批决定后，可以关闭低层级人员的审批任务", // Description
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 0.9f
        //        };

        //        var approvers = new IUser[] {
        //        new User() { Email = "manager@test.com" },
        //        new User() { Email = "director@test.com" },
        //        new User() { Email = "vp@test.com" }
        //    };
        //        var caTasks = project.CreateApprovalFlow(task, approvers);
        //        caTasks[approvers.Length - 1].Close();

        //        foreach (var caTask in caTasks)
        //            Assert.IsNotNull(caTask.ClosedDate);

        //        task = new Task()
        //        {
        //            Title = "OverrideApprovalTest_2",  // Title
        //            Description = "测试具有高层级审批权限的人员做出审批决定后，可以关闭低层级人员的审批任务", // Description
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 0.9f
        //        };

        //        caTasks = project.CreateApprovalFlow(task, approvers);
        //        caTasks[1].Close();

        //        for (int i = 0; i < 2; ++i)
        //            Assert.IsNotNull(caTasks[i].ClosedDate);

        //        for (int i = 2; i < caTasks.Length; ++i)
        //            Assert.IsNull(caTasks[i].ClosedDate);
        //    }
        //}

        //[TestMethod]
        //public void ActiveApprovedTaskTest()
        //{
        //    using (var impl = new VoweiContextImpl())
        //    {
        //        var project = GetSvnProject(impl);;
        //        var task = new Task()
        //        {
        //            Title = "ActiveApprovedTaskTest",  // Title
        //            Description = "测试一个审批流程完成后，其他成员可以重新激活审批流程", // Description
        //            DueDate = DateTime.Now + TimeSpan.FromDays(1),
        //            Progress = 0.9f
        //        };

        //        var approvers = new IUser[] {
        //            new Employee() { Email = "manager@test.com" },
        //            new Employee() { Email = "director@test.com" },
        //            new Employee() { Email = "vp@test.com" }
        //        };

        //        var caTasks = project.CreateApprovalFlow(task, approvers);
        //        caTasks[approvers.Length - 1].Close();

        //        foreach (var caTask in caTasks)
        //            caTask.Close();

        //        foreach (var caTask in caTasks)
        //            Assert.IsNotNull(caTask.ClosedDate);

        //        // 激活流程
        //        caTasks[1].Active();
        //        for (int i = 0; i < 1; ++i)
        //            Assert.IsNotNull(caTasks[i].ClosedDate);

        //        // 重新激活的节点以上需要再次审批
        //        for (int i = 1; i < caTasks.Length; ++i)
        //            Assert.IsNull(caTasks[i].ClosedDate);
        //    }
        //}
    }

}
